<?php
class ModuleManager {
	
	/**
	 * This Method tries to load the ModuleClass from the Modules directories
	 * @param $name - Name of the Module
	 * @return eCMSModule
	 */
	
	public static function getModule($name) {
		
		$list = new ModuleList();
		$list->GetByName($name);
		
		if(count($list)==1) {
			
			$cfg = Runtime::getConfiguration();
			
			$item = $list->At(0);
			
			//Try to locate the correct File
			$filename = $name . "Module.class.php";
			$paths = array(
				$cfg["paths"]["page"] . "/module/core/$name",
				$cfg["paths"]["page"] . "/module/user/$name",
				eCMS_DIR . "/module/core/$name",
				eCMS_DIR . "/module/user/$name",
				);
			
			$file = Utils::findFile($filename, $paths);
			
			if(is_file($file)) {
				
				//Try to include
				require_once($file);
				$classname = $name."Module";
				
				$module = new $classname;
				$module->setModuleFilePath($file);
				
				return $module;
			}
			else {
				throw new eCMSException("File '$file' not found. Filename: $filename", "ModuleFileMissing");
			}
			
		}
		else {
			throw new eCMSException("Module '$name' not found", "ModuleNotFound");
		}
		
		
	}
	
}
?>